import { api, apiClient, ApiResponse } from './client'
import { Sheet, Cell, CellStyle } from '@/types/sheet'

// 表格相关 API
export const sheetsApi = {
  // 获取用户的表格列表
  getUserSheets: (): Promise<ApiResponse<Sheet[]>> =>
    api.get('/sheets'),

  // 创建新表格
  createSheet: (data: { title: string; description?: string }): Promise<ApiResponse<Sheet>> =>
    api.post('/sheets', data),

  // 获取表格详情
  getSheetById: (sheetId: string): Promise<ApiResponse<Sheet>> =>
    api.get(`/sheets/${sheetId}`),

  // 更新表格信息
  updateSheet: (sheetId: string, data: { title?: string; description?: string }): Promise<ApiResponse<Sheet>> =>
    api.put(`/sheets/${sheetId}`, data),

  // 删除表格
  deleteSheet: (sheetId: string): Promise<ApiResponse<void>> =>
    api.delete(`/sheets/${sheetId}`),

  // 更新单元格
  updateCell: (sheetId: string, data: { row: number; column: number; value: string }): Promise<ApiResponse<Cell>> =>
    api.put(`/sheets/${sheetId}/cells`, data),

  // 更新单元格样式
  updateCellStyle: (sheetId: string, data: { row: number; column: number; style: CellStyle }): Promise<ApiResponse<Cell>> =>
    api.put(`/sheets/${sheetId}/cells/style`, data),

  // 批量更新单元格
  batchUpdateCells: (sheetId: string, cells: Cell[]): Promise<ApiResponse<void>> =>
    api.put(`/sheets/${sheetId}/cells/batch`, { cells }),

  // 保存表格
  saveSheet: (sheetId: string, data: { cells: Cell[] }): Promise<ApiResponse<void>> =>
    api.post(`/sheets/${sheetId}/save`, data),

  // 分享表格
  shareSheet: (sheetId: string, data: { allowEdit?: boolean; expiresAt?: string }): Promise<ApiResponse<{ shareToken: string; shareUrl: string }>> =>
    api.post(`/sheets/${sheetId}/share`, data),

  // 获取分享信息
  getShareInfo: (sheetId: string): Promise<ApiResponse<{ shareToken: string; shareUrl: string; allowEdit: boolean; expiresAt: string | null }>> =>
    api.get(`/sheets/${sheetId}/share`),

  // 取消分享
  unshareSheet: (sheetId: string): Promise<ApiResponse<void>> =>
    api.delete(`/sheets/${sheetId}/share`),

}

export default sheetsApi
